// 云函数入口文件
const cloud = require('wx-server-sdk');

cloud.init();

const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
  const { type, docId, updatedFields } = event;

  if (type === 'update' && updatedFields.hasOwnProperty('futureFormattedDate') && updatedFields.futureFormattedDate === '0') {
    try {
      await db.collection('user').doc(docId).update({
        data: {
          vip: false
        }
      });

      return {
        success: true
      };
    } catch (err) {
      console.error(err);
      return {
        success: false,
        error: err
      };
    }
  }
};
